package Dscauhoi.backing;

import Dscauhoi.Dscauhoi;

import Dscauhoi.resource.DscauhoiBundle;

import Dschude.DsChudeBean;

import common.Utils;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import javax.faces.context.FacesContext;

import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.PortletMode;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequest;
import javax.portlet.WindowState;

import oracle.adf.model.binding.DCIteratorBinding;

import oracle.jbo.ViewCriteria;
import oracle.jbo.ViewCriteriaRow;
import oracle.jbo.ViewObject;
import oracle.jbo.uicli.binding.JUCtrlRangeBinding;

public class DscauhoiBacking {
    private String mPortletTitle;
    private String chudeId;
    private String tieude;
    private String chitieturl;
    private String pageDsCauhoiUrl;
    private String langid;
    private String tieudeDscauhoi;
    private int pageNumber;
    private static final int RANGE_SIZE = 10;
    private String viewRender;
    private boolean paging = false;

    public String getViewRender() {
        getChudeId();
        if (chudeId != null) {
            getDsCauHoi(chudeId);
            getChudeKhac(chudeId);
        }
        return viewRender;
    }

    private void getDsCauHoi(String chudeid) {
        DCIteratorBinding hoidapIter =
            Utils.getIteratorBinding("HoidapView1Iterator");
        ViewObject vo = hoidapIter.getViewObject();
        ViewCriteria vc = vo.createViewCriteria();
        ViewCriteriaRow cr = vc.createViewCriteriaRow();
        String varStr = "=" + chudeid;
        cr.setAttribute("LinhvucId", varStr);
        vc.addElement(cr);
        vo.getViewCriteriaManager().clearViewCriterias();
        vo.applyViewCriteria(vc);
        vo.executeQuery();
        checkPaging();
    }

    private void getChudeKhac(String chudeid) {
        DCIteratorBinding linhvucIter =
            Utils.getIteratorBinding("HoidapLinhvucView1Iterator");
        ViewObject vo = linhvucIter.getViewObject();
        ViewCriteria vc =
            vo.getViewCriteriaManager().getViewCriteria("HoidapLinhvucViewCriteria1");
        vo.ensureVariableManager().setVariableValue("varchudeid", chudeid);
        vo.ensureVariableManager().setVariableValue("varLang", langid);
        vo.ensureVariableManager().setVariableValue("varNgayhethan",
                                                    Utils.getCurrentDate().dateValue().toString());
        vo.getViewCriteriaManager().removeApplyViewCriteriaName("HoidapLinhvucViewCriteria1");
        vo.applyViewCriteria(vc);
        vo.executeQuery();
    }

    public String getPortletTitle() {
        if (mPortletTitle == null) {
            PortletRequest request =
                (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
            PortletPreferences preferences = request.getPreferences();
            mPortletTitle =
                    preferences.getValue(Dscauhoi.PORTLETTITLE_KEY, getResource(DscauhoiBundle.PORTLETTITLE));
        }
        return mPortletTitle;
    }

    private void checkPaging() {
        long lastPageNum1 =
            Math.max(1, (getHoidapView().getEstimatedRowCount() - 1) /
                     RANGE_SIZE + 1);
        if (lastPageNum1 > 1) {
            setPaging(true);
        }
    }

    public String getPortletTitleLabel() {
        String resource = getResource(DscauhoiBundle.PORTLETTITLE);
        return resource;
    }

    public void setPortletTitle(String portletTitle) {
        mPortletTitle = portletTitle;
    }

    public String applyAction() throws Exception {
        PortletRequest request =
            (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        PortletPreferences preferences = request.getPreferences();
        preferences.setValues(Dscauhoi.PORTLETTITLE_KEY,
                              mPortletTitle.split(","));
        preferences.store();
        return null;
    }

    public String getApplyLabel() {
        String resource = getResource(DscauhoiBundle.APPLY_LABEL);
        return resource;
    }

    public String okAction() throws Exception {
        applyAction();
        ActionResponse response =
            (ActionResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
        response.setPortletMode(PortletMode.VIEW);
        response.setWindowState(WindowState.NORMAL);
        return null;
    }

    public String getOkLabel() {
        String resource = getResource(DscauhoiBundle.OK_LABEL);
        return resource;
    }

    public String getTieuDeCauHoi() {
        String resource = getResource("TIEU_DE_CAU_HOI");
        return resource;
    }

    public String getCacChuDeKhac() {
        String resource = getResource("CAC_CHU_DE_KHAC");
        return resource;
    }

    public String getNoiDungCauHoiLabel() {
        String resource = getResource("NOI_DUNG_CAU_HOI");
        return resource;
    }

    public String getTrangdauLabel() {
        String resource = getResource("TRANG_DAU");
        return resource;
    }

    public String getTrangtruocLabel() {
        String resource = getResource("TRUOC");
        return resource;
    }

    public String getTrangsauLabel() {
        String resource = getResource("SAU");
        return resource;
    }

    public String getTrangcuoiLabel() {
        String resource = getResource("TRANG_CUOI");
        return resource;
    }

    private String getResource(String resourceKey) {
        Locale locale = getPortalLocale();
        ResourceBundle bundle =
            ResourceBundle.getBundle("model.PortletsBundle", locale);
        return bundle.getString(resourceKey);
    }

    /**
     * Author       Date        Comment
     *  TớiNT    09/11/2012     Thêm mới
     * @return Kiểu ngôn ngữ
     */
    private Locale getPortalLocale() {
        Locale portalLocale = new Locale("vi", "VN");
        String language = "";
        try {
            PortletRequest request =
                (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
            if (request.getParameter("ngonnguid") != null) {
                language = request.getParameter("ngonnguid");
                portalLocale = new Locale(language);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return portalLocale;
    }

    /**
     * Author       Date        Comment
     *  TớiNT    09/11/2012     Thêm mới
     * @return request parameter chudeid
     */
    private String ChudeId() {
        String chudeId = "1";
        try {
            PortletRequest request =
                (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
            if (request.getParameter("chudeid") != null)
                chudeId = request.getParameter("chudeid").toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return chudeId;
    }

    /**
     * Author       Date        Comment
     *  TớiNT    09/11/2012     Thêm mới
     * @return url chi tiết câu h�?i
     */
    private String hoidapchitietUrl() {
        String hoidapchitietUrl = "";
        try {
            PortletRequest request =
                (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
            if (request.getParameter("ChitietUrl") != null)
                hoidapchitietUrl =
                        request.getParameter("ChitietUrl").toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return hoidapchitietUrl;
    }

    private String TieudeDscauhoi() {
        String tieudeDscauhoi = "Danh sách câu h�?i";
        try {

            PortletRequest request =
                (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
            if (request.getParameter("tieudedscauhoi") != null)
                tieudeDscauhoi =
                        request.getParameter("tieudedscauhoi").toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return tieudeDscauhoi;
    }

    /**
     * Author       Date        Comment
     *  TớiNT    09/11/2012     Thêm mới
     * @return url danh sách câu h�?i
     */
    private String UrlDsCauHoi() {
        String url = "";
        try {
            PortletRequest request =
                (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
            if (request.getParameter("dsCauhoiUrl") != null)
                url = request.getParameter("dsCauhoiUrl").toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return url;
    }

    public void setChudeId(String chudeId) {
        this.chudeId = chudeId;
    }

    public String getChudeId() {
        getLangid();
        chudeId = ChudeId();
        chitieturl = hoidapchitietUrl();
        pageDsCauhoiUrl = UrlDsCauHoi();
        return chudeId;
    }

    public void setChitieturl(String chitieturl) {

        this.chitieturl = chitieturl;
    }

    public String getChitieturl() {
        return chitieturl;
    }

    public void setPageDsCauhoiUrl(String pageDsCauhoiUrl) {
        this.pageDsCauhoiUrl = pageDsCauhoiUrl;
    }

    public String getPageDsCauhoiUrl() {
        return pageDsCauhoiUrl;
    }

    public String getLangid() {
        langid = this.getPortalLocale().getLanguage().toString();
        return langid;
    }

    public void setTieudeDscauhoi(String tieudeDscauhoi) {
        this.tieudeDscauhoi = tieudeDscauhoi;
    }

    public String getTieudeDscauhoi() {
        tieudeDscauhoi = TieudeDscauhoi();
        return tieudeDscauhoi;
    }

    public void setTieude(String tieude) {
        this.tieude = tieude;
    }

    public String getTieude() {
        return tieude;
    }

    private JUCtrlRangeBinding getHoidapView() {
        return Utils.getViewObject("HoidapView1");
    }

    public String goLastPage() {
        Utils.goLastPage(RANGE_SIZE, "HoidapView1");
        return null;
    }

    public boolean isLastEnabled() {
        return Utils.isLastEnabled(RANGE_SIZE, "HoidapView1");
    }

    public boolean isFirstEnabled() {
        return Utils.isFirstEnabled(RANGE_SIZE, "HoidapView1");
    }

    public void setPageNumber(int pageNumber) {
        JUCtrlRangeBinding hoidapView = this.getHoidapView();
        if (pageNumber >
            Math.max(1, (hoidapView.getIteratorBinding().getEstimatedRowCount() -
                         1) / RANGE_SIZE + 1)) {
            pageNumber =
                    (int)Math.max(1, (hoidapView.getIteratorBinding().getEstimatedRowCount() -
                                      1) / RANGE_SIZE + 1);
        }
        this.pageNumber = pageNumber;
    }

    public int getPageNumber() {
        return pageNumber;
    }

    public void setPaging(boolean paging) {
        this.paging = paging;
    }

    public boolean isPaging() {
        return paging;
    }
}
